package org.glow.changgou.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.glow.changgou.goods.dao.SpecMapper;
import org.glow.changgou.goods.pojo.Category;
import org.glow.changgou.goods.pojo.Spec;
import org.glow.changgou.goods.service.CategoryService;
import org.glow.changgou.goods.service.SpecService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author Glow
 * @Date 2022-09-22 20:17:21
 * @Description
 * @Version 1.0
 */
@Service
public class SpecServiceImpl extends ServiceImpl<SpecMapper, Spec> implements SpecService {
    @Autowired
    private SpecService specService;
    @Autowired
    private CategoryService categoryService;
    @Override
    public Page<Spec> findPage(Spec spec, int page, int size) {
        LambdaQueryWrapper<Spec> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,spec);
        Page<Spec> pageInfo = new Page<>(page,size);
        return specService.page(pageInfo,queryWrapper);
    }

    @Override
    public Page<Spec> findPage(int page, int size) {
        Page<Spec> pageInfo = new Page<>(page,size);
        return specService.page(pageInfo);
    }

    @Override
    public List<Spec> findList(Spec spec) {
        LambdaQueryWrapper<Spec> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper = getCondition(queryWrapper,spec);
        return specService.list(queryWrapper);
    }

    @Override
    public void delete(Integer id) {
        specService.removeById(id);
    }

    @Override
    public void update(Spec spec) {
        specService.update(spec);
    }

    @Override
    public void add(Spec spec) {
        specService.save(spec);
    }

    @Override
    public Spec findById(Integer id) {
        return specService.getById(id);
    }

    @Override
    public List<Spec> findAll() {
        return specService.list();
    }

    @Override
    public List<Spec> findByCategoryId(Integer categoryid) {
        Category category = categoryService.getById(categoryid);
        LambdaQueryWrapper<Spec> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Spec::getTemplateId,category.getTemplateId());
        return specService.list(queryWrapper);
    }

    public LambdaQueryWrapper<Spec> getCondition( LambdaQueryWrapper<Spec> queryWrapper,Spec spec){

        if(spec!=null){
            // ID
            if(!StringUtils.isEmpty(spec.getId())){
//                criteria.andEqualTo("id",spec.getId());
                queryWrapper.eq(Spec::getId,spec.getId());
            }
            // 名称
            if(!StringUtils.isEmpty(spec.getName())){
//                criteria.andLike("name","%"+spec.getName()+"%");
                queryWrapper.like(Spec::getName,spec.getName());
            }
            // 规格选项
            if(!StringUtils.isEmpty(spec.getOptions())){
//                criteria.andEqualTo("options",spec.getOptions());
                queryWrapper.eq(Spec::getOptions,spec.getOptions());
            }
            // 排序
            if(!StringUtils.isEmpty(spec.getSeq())){
//                criteria.andEqualTo("seq",spec.getSeq());
                queryWrapper.eq(Spec::getSeq,spec.getSeq());
            }
            // 模板ID
            if(!StringUtils.isEmpty(spec.getTemplateId())){
//                criteria.andEqualTo("templateId",spec.getTemplateId());
                queryWrapper.eq(Spec::getTemplateId,spec.getTemplateId());
            }
        }
        return queryWrapper;
    }
}
